using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Portal.User.Security;
using System.Web.Caching;
using Portal.BO.Editoral.Newslist;
using Portal.BO.Editoral.Category;
using Portal.BO.Editoral.PhotoAlbum;
using Portal.BO.Editoral.CommentManagement;

namespace Portal.GUI.EditoralOffice.MainOffce.Menu
{
    public partial class MnuEditor : System.Web.UI.UserControl
    {
        #region private method
        private bool IsReadyAlbum = false;
        private bool IsReadyNews = false;
        private void CreateMenuNews()
        {
            DataTable table = CommonHelper.GetDataPermission(RolePrefix.THUKYTOANSOAN, 1);
            DataView dv = table.DefaultView;
            dv.Sort = "Permission_ID";
            //dlMenuNews.DataSource = dv;
            //dlMenuNews.DataBind();
            dtlMenuNews.DataSource = dv;
            dtlMenuNews.DataBind();
            if (table.Rows.Count == 0)
            {
                table1.Visible = false;
            }
        }
        private void CreateMenuAlbum()
        {
            DataTable table = CommonHelper.GetDataPermission(RolePrefix.THUKYTOANSOAN, 2);
            DataView dv = table.DefaultView;
            dv.Sort = "Permission_ID";
            //dlMenuAlbum.DataSource = dv;
            //dlMenuAlbum.DataBind();
            dtlMenuAlbum.DataSource = dv;
            dtlMenuAlbum.DataBind();
            if (table.Rows.Count == 0)
            {
                //table2.Visible = false;
            }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                /*string strCacheName = "IsTBTOrPTK" + Page.User.Identity.Name;
                if (HttpContext.Current.Cache[strCacheName] == null)
                {
                    MainSecurity objSecu = new MainSecurity();
                    Role objPermis = objSecu.GetRole(Page.User.Identity.Name, Portal.API.Config.CurrentChannel);
                    if (objPermis.isTongBienTap || objPermis.isPhuTrachKenh)
                    {
                        SaveCache(strCacheName, "false");
                        Div1.Visible = false;
                    }
                    else
                    {
                        SaveCache(strCacheName, "true");
                    }
                }
                else
                {
                    bool isOk = Convert.ToBoolean(HttpContext.Current.Cache[strCacheName].ToString());
                    Div1.Visible = isOk;
                }*/

                
                CreateMenuAlbum();
                CreateMenuNews();
                //string strCats = CategoryHelper.GetCatIDByUser();
                //if (strCats != null && strCats != "")
                //    ltrComment.Text = "(<font color=red><b>" + Portal.BO.Editoral.CommentManagement.CommentManagementHelper.GetRowCount(" AND Cat_ID IN (" + strCats + ") ", "baivietmoi") + "</b></font>)";
            }
        }

        private void SaveCache(string strCacheName, string value)
        {
            HttpContext.Current.Cache.Add(strCacheName, value, null, DateTime.Now.AddHours(24), TimeSpan.Zero, CacheItemPriority.High, null);
        }
        #region dlMenuNews_ItemDataBound
        protected void dlMenuNews_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal ltrCount = (Literal)e.Item.FindControl("ltrCount");
                HiddenField IsCount = (HiddenField)e.Item.FindControl("IsCount");
                HiddenField Cpmode = (HiddenField)e.Item.FindControl("Cpmode");
                HtmlGenericControl divItem = (HtmlGenericControl)e.Item.FindControl("divItem");
                String strCpmode = Request.QueryString["cpmode"];




                //if (strCpmode == Cpmode.Value)
                //{
                //    divItem.Attributes.Remove("class");
                //    divItem.Attributes.Add("class", "Menuleft_Item_Select");
                //}
                //else
                if (Session["cpmode"] != null)
                {
                    String cpmodeSession = Session["cpmode"].ToString();
                    if (cpmodeSession == Cpmode.Value)
                    {
                        divItem.Attributes.Remove("class");
                        divItem.Attributes.Add("class", "Menuleft_Item_Select");
                    }
                }

                if (IsCount.Value.Equals("True"))
                {
                    ltrCount.Text = " (<font color=red><b>" + NewslistHelper.GetCountNews(Cpmode.Value) + "</b></font>)";
                }
            }
        }
        #endregion
        #region dlMenuAlbum_ItemDataBound
        protected void dlMenuAlbum_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal ltrCount = (Literal)e.Item.FindControl("ltrCount");
                HiddenField IsCount = (HiddenField)e.Item.FindControl("IsCount");
                HiddenField Cpmode = (HiddenField)e.Item.FindControl("Cpmode");
                HtmlGenericControl divItem = (HtmlGenericControl)e.Item.FindControl("divItem");
                String strCpmode = Request.QueryString["cpmode"];
                //if ((Cpmode.Value == strCpmode))

                
                //if (Cpmode.Value == strCpmode)
                //{
                //    divItem.Attributes.Remove("class");
                //    divItem.Attributes.Add("class", "Menuleft_Item_Select");
               //     IsReadyAlbum = true;
                //}
                //else 
                if (Session["cpmode"] != null)
                {
                    String cpmodeSession = Session["cpmode"].ToString();
                    if (cpmodeSession == Cpmode.Value)
                    {
                        divItem.Attributes.Remove("class");
                        divItem.Attributes.Add("class", "Menuleft_Item_Select");
                        IsReadyAlbum = true;
                    }
                }

                if (IsCount.Value.Equals("True"))
                {
                    ltrCount.Text = " (<font color=red><b>" + PhotoAlbumHelper.CountPhotoAlbums(Cpmode.Value) + "</b></font>)";
                }
            }
        }
        #endregion

        protected void dlMenuAlbum_ItemCommand(object source, DataListCommandEventArgs e)
        {
            Session["cpmode"] = e.CommandName;
            Response.Redirect(e.CommandArgument.ToString());
        }
    }
}